"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FormListContext = void 0;
exports.ProFormList = ProFormList;

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _jsxRuntime = require("react/jsx-runtime");

var _icons = require("@ant-design/icons");

var _proUtils = require("@ant-design/pro-utils");

var _antdV = require("antd-v4");

var _warning = require("rc-util/lib/warning");

var _react = _interopRequireWildcard(require("react"));

var _helpers = require("../../helpers");

require("./index.less");

var _ListContainer = require("./ListContainer");

var _excluded = ["actionRender", "creatorButtonProps", "label", "alwaysShowItemLabel", "tooltip", "creatorRecord", "itemRender", "rules", "itemContainerRender", "fieldExtraRender", "copyIconProps", "children", "deleteIconProps", "actionRef", "style", "prefixCls", "actionGuard", "min", "max", "colProps", "rowProps"];

var FormListContext = /*#__PURE__*/_react.default.createContext({});

exports.FormListContext = FormListContext;

function ProFormList(_ref) {
  var actionRender = _ref.actionRender,
      creatorButtonProps = _ref.creatorButtonProps,
      label = _ref.label,
      alwaysShowItemLabel = _ref.alwaysShowItemLabel,
      tooltip = _ref.tooltip,
      creatorRecord = _ref.creatorRecord,
      itemRender = _ref.itemRender,
      rules = _ref.rules,
      itemContainerRender = _ref.itemContainerRender,
      fieldExtraRender = _ref.fieldExtraRender,
      _ref$copyIconProps = _ref.copyIconProps,
      copyIconProps = _ref$copyIconProps === void 0 ? {
    Icon: _icons.CopyOutlined,
    tooltipText: '复制此行'
  } : _ref$copyIconProps,
      _children = _ref.children,
      _ref$deleteIconProps = _ref.deleteIconProps,
      deleteIconProps = _ref$deleteIconProps === void 0 ? {
    Icon: _icons.DeleteOutlined,
    tooltipText: '删除此行'
  } : _ref$deleteIconProps,
      actionRef = _ref.actionRef,
      style = _ref.style,
      prefixCls = _ref.prefixCls,
      actionGuard = _ref.actionGuard,
      min = _ref.min,
      max = _ref.max,
      colProps = _ref.colProps,
      rowProps = _ref.rowProps,
      rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var actionRefs = (0, _react.useRef)();
  var context = (0, _react.useContext)(_antdV.ConfigProvider.ConfigContext);
  var listContext = (0, _react.useContext)(FormListContext);
  var baseClassName = context.getPrefixCls('pro-form-list');

  var _useGridHelpers = (0, _helpers.useGridHelpers)({
    colProps: colProps,
    rowProps: rowProps
  }),
      ColWrapper = _useGridHelpers.ColWrapper,
      RowWrapper = _useGridHelpers.RowWrapper; // 处理 list 的嵌套


  var name = (0, _react.useMemo)(function () {
    if (listContext.name === undefined) {
      return [rest.name].flat(1);
    }

    return [listContext.name, rest.name].flat(1);
  }, [listContext.name, rest.name]);
  var proFormContext = (0, _react.useContext)(_proUtils.ProFormContext); // eslint-disable-next-line react-hooks/exhaustive-deps

  (0, _react.useImperativeHandle)(actionRef, function () {
    return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, actionRefs.current), {}, {
      get: function get(index) {
        return proFormContext.formRef.current.getFieldValue([].concat((0, _toConsumableArray2.default)(name), [index]));
      },
      getList: function getList() {
        return proFormContext.formRef.current.getFieldValue((0, _toConsumableArray2.default)(name));
      }
    });
  }, [name, proFormContext.formRef]);
  (0, _react.useEffect)(function () {
    (0, _warning.noteOnce)(!!proFormContext.formRef, "ProFormList \u5FC5\u987B\u8981\u653E\u5230 ProForm \u4E2D,\u5426\u5219\u4F1A\u9020\u6210\u884C\u4E3A\u5F02\u5E38\u3002");
    (0, _warning.noteOnce)(!!proFormContext.formRef, "Proformlist must be placed in ProForm, otherwise it will cause abnormal behavior.");
  }, [proFormContext.formRef]);
  if (!proFormContext.formRef) return null;
  return (0, _jsxRuntime.jsx)(ColWrapper, {
    children: (0, _jsxRuntime.jsx)("div", {
      className: baseClassName,
      style: style,
      children: (0, _jsxRuntime.jsx)(_antdV.Form.Item, (0, _objectSpread2.default)((0, _objectSpread2.default)({
        label: label,
        prefixCls: prefixCls,
        tooltip: tooltip,
        style: style
      }, rest), {}, {
        name: undefined,
        rules: undefined,
        children: (0, _jsxRuntime.jsx)(_antdV.Form.List, (0, _objectSpread2.default)((0, _objectSpread2.default)({
          rules: rules
        }, rest), {}, {
          name: name,
          children: function children(fields, action, meta) {
            // 将 action 暴露给外部
            actionRefs.current = action;
            return (0, _jsxRuntime.jsxs)(RowWrapper, {
              children: [(0, _jsxRuntime.jsx)(_ListContainer.ProFormListContainer, {
                name: name,
                originName: rest.name,
                copyIconProps: copyIconProps,
                deleteIconProps: deleteIconProps,
                formInstance: proFormContext.formRef.current,
                prefixCls: baseClassName,
                meta: meta,
                fields: fields,
                itemContainerRender: itemContainerRender,
                itemRender: itemRender,
                fieldExtraRender: fieldExtraRender,
                creatorButtonProps: creatorButtonProps,
                creatorRecord: creatorRecord,
                actionRender: actionRender,
                action: action,
                actionGuard: actionGuard,
                alwaysShowItemLabel: alwaysShowItemLabel,
                min: min,
                max: max,
                count: fields.length,
                children: _children
              }), (0, _jsxRuntime.jsx)(_antdV.Form.ErrorList, {
                errors: meta.errors
              })]
            });
          }
        }))
      }))
    })
  });
}